﻿/*
 * OpenDoPE Word Add-In
 * Copyright (C) 2010-2011  Plutext Pty Ltd
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * For details, see http://www.gnu.org/licenses/gpl-2.0.html
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NLog;

using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;

namespace OpenDoPE_Wed
{
    public partial class FormXPathPartViewer : Form
    {
        static Logger log = LogManager.GetLogger("OpenDoPE_Wed");

        public XmlEditorControl xmlEditor { get; set; }

        private Dictionary<string, string> existingXPaths = new Dictionary<string, string>();

        //private Office.CustomXMLPart xpathsPart;

        WedTaskPane taskPane;

        int editorWidth = 300; // hard coded on this form, for now


        public FormXPathPartViewer(WedTaskPane taskPane) //Office.CustomXMLPart xpathsPart)
        {
            this.taskPane = taskPane;

            InitializeComponent();

            if (taskPane.questions)
            {
                // Make a note of IDs
                xpaths xpaths = new xpaths();
                xpaths.Deserialize(taskPane.xpathsPart.XML, out xpaths);
                foreach (xpathsXpath xp in xpaths.xpath)
                {
                    existingXPaths.Add(xp.id, xp.id);
                }
            }

            this.xmlEditor = new XmlEditorControl(taskPane.xpathsPart, ".xpaths", false, editorWidth);
            this.xmlEditor.Location = new Point(10, 70);
            this.xmlEditor.Size = new System.Drawing.Size(editorWidth, 400); // width, height
            this.Controls.Add(xmlEditor);
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (xmlEditor.isWellFormed())
            {
                xmlEditor.commitChanges();

                if (taskPane.questions)
                {
                    // Have any XPaths been added?
                    bool dirty = false;
                    xpaths newxpaths = new xpaths();
                    xpaths.Deserialize(taskPane.xpathsPart.XML, out newxpaths);
                    foreach (xpathsXpath xp in newxpaths.xpath)
                    {
                        try
                        {
                            string foo = existingXPaths[xp.id];
                        }
                        catch (KeyNotFoundException knfe)
                        {
                            string questionID = null;
                            FormQuestion formQuestion = new FormQuestion(taskPane.questionsPart,
                                xp.dataBinding.xpath);
                            formQuestion.ShowDialog();
                            // TODO - handle cancel

                            formQuestion.updateQuestionsPart(formQuestion.getQuestion() );

                            questionID = formQuestion.textBoxQID.Text;
                            formQuestion.Dispose();

                            xp.questionID = questionID;
                            dirty = true;

                            existingXPaths.Add(xp.id, xp.id);
                            // In case they hit save again ...
                        }
                    }

                    if (dirty)
                    {
                        CustomXmlUtilities.replaceXmlDoc(taskPane.xpathsPart, newxpaths.Serialize());
                    }
                }
            }
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
